package com.jinxin.platform.cdsmsmgr.controller;

import com.jinxin.platform.base.api.annotation.APCOSApi;
import com.jinxin.platform.base.api.annotation.APCOSApiOperation;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SmsBusinessInfo;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SysUserVO;
import com.jinxin.platform.cdsmsmgr.pojo.entity.Constant;
import com.jinxin.platform.cdsmsmgr.pojo.entity.ResultEntity;
import com.jinxin.platform.cdsmsmgr.rpc.SysUserRpc;
import com.jinxin.platform.cdsmsmgr.service.ISmsBusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-06-29
 * @Author: Lingnan
 **/
@APCOSApi(value = "模板配置接口", developers = {"那个男人"}, description = "模板配置接口")
@RestController
@RequestMapping("/cdsmsmgr/smsBusiness")
@Slf4j
public class SmsBusinessController {
    @Autowired
    private ISmsBusinessService businessService;
    @Autowired
    private SysUserRpc sysUserRpc;

    @APCOSApiOperation(value = "分页获取模板配置", developers = {"lingnan"}, description = "分页获取模板配置",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/list")
    public Map<String,Object> list(@RequestParam long page, @RequestParam long limit,
                                   @RequestParam(name = "name",required = false)String name,
                                   @RequestParam(name = "accountType") Integer accountType){
        SysUserVO currentUser = sysUserRpc.getCurrentUser();
        if (currentUser == null) {
            log.error("未登录--,smsBusiness list接口");
            return null;
        }
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        param.put("name", name);
        String userAccount = currentUser.getUserAccount();
        if(!Constant.ACCOUNT_ADMIN.equals(userAccount)){
            param.put("applyUser", userAccount);
        }
        param.put("accountType", accountType);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("count", businessService.listPageCount(param));
        resultMap.put("data", businessService.listPage(param));
        return resultMap;
    }

    @APCOSApiOperation(value = "保存或更新短信业务", developers = {"lingnan"}, description = "保存或更新短信业务",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @PostMapping("/saveInfo")
    public ResultEntity saveInfo(SmsBusinessInfo businessInfo){
        return businessService.saveOrUpdate(businessInfo);
    }

    @APCOSApiOperation(value = "获取短信业务列表", developers = {"lingnan"}, description = "获取短信业务列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/loadSmsBusinessList")
    public JsonResult loadSmsBusinessList(String dictType){
        if(StringUtils.isEmpty(dictType)){
            return null;
        }
        return sysUserRpc.getSmsBusinessList(dictType);
    }

    @APCOSApiOperation(value = "删除模板配置", developers = {"lingnan"}, description = "删除模板配置",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/delete")
    public ResultEntity delete(String id) {
        return businessService.deleteById(id);
    }

}
